﻿var Winsmarts = {};
Winsmarts.SP = {};
Winsmarts.SP.RestProxy = new function () {
    var internalFunction = function () {

    };

    this.executeAsync = function (requestInfo) {
        if (requestInfo.method == "GET") {
            var url = "/Services/S2SProxy/endpoint.svc/ExecuteGet?getURL=" + encodeURIComponent(requestInfo.url) + "&" + Math.random();
            $.ajax({
                url: url,
                method: requestInfo.method,
                headers: requestInfo.headers,
                success: function (data) {
                    var toPassObject = {};
                    toPassObject.body = data;
                    requestInfo.success(toPassObject);
                },
                error: requestInfo.error
            });
        } else if (requestInfo.method = "POST") {
            var toSend = {};
            toSend.postURL = encodeURIComponent(requestInfo.url);
            if (requestInfo.body == undefined || requestInfo.body == null) {
                toSend.postBody = "NO BODY REQUIRED";
            } else toSend.postBody = requestInfo.body;
            
            var url = "/Services/S2SProxy/endpoint.svc/ExecutePost";
            $.ajax({
                url: url,
                method: requestInfo.method,
                contentType:"application/json; odata=verbose",
                data: JSON.stringify(toSend),
                headers: requestInfo.headers,
                success: function (data) {
                    var toPassObject = {};
                    toPassObject.body = data;
                    requestInfo.success(toPassObject);
                },
                error: requestInfo.error
            });
        }
    };
};